using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using AJAXExtenders;

namespace Controls
{
    public class SmartTextBox : TextBox
    {
        public RequiredFieldValidator validator = new RequiredFieldValidator();
        public RequiredFieldExtender rfe = new RequiredFieldExtender();
        public Label label = new Label();

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            //Add validator
            validator.ID = this.ID + "_ReqValidator";
            validator.ControlToValidate = base.ID;
            validator.ErrorMessage = "Required";
            this.Controls.Add(validator);

            //Add Label
            label.Text = "Custom Control: ";
            label.ID = this.ID + "_Label";
            this.Controls.AddAt(0,label);

            //Add required field extender
            rfe.ID = this.ID + "_ReqValidatorExtendear";
            rfe.TargetControlID = validator.ID;
            rfe.TargetLabelID = label.ID;
            rfe.InvalidTextBoxCssClass = "invalidtextbox";
            rfe.InvalidLabelCssClass = "invalidlabel";
            this.Controls.Add(rfe);
        }

        protected override void Render(HtmlTextWriter writer)
        {
            base.Render(writer);
            validator.RenderControl(writer);
            rfe.RenderControl(writer);
            label.RenderControl(writer);
        }

    }
}
